package com.syh.jn.scm.service.impl.supplier.finance;

import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.finance.PurchaseInvoice;
import com.syh.jn.scm.domain.supplier.event.finance.PurchaseInvoiceEvent;
import com.syh.jn.scm.domain.supplier.repository.finance.PurchaseInvoiceRepository;
import com.syh.jn.scm.dto.supplier.finance.PurchaseInvoiceMto;
import com.syh.jn.scm.dto.supplier.finance.PurchaseInvoiceSto;
import com.syh.jn.scm.dto.supplier.finance.PurchaseInvoiceVo;
import com.syh.jn.scm.service.api.supplier.finance.PurchaseInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 进项发票接口实现
 */
@Service
@Transactional
public class PurchaseInvoiceServiceImpl extends AbstractServiceImpl<PurchaseInvoiceSto, PurchaseInvoiceMto, PurchaseInvoiceVo, PurchaseInvoice, Long> implements PurchaseInvoiceService {

  @Autowired
  private PurchaseInvoiceRepository purchaseInvoiceRepositoryImpl;

  @Override
  protected AggregateRepository getAggregateRepository() {
    return purchaseInvoiceRepositoryImpl;
  }

  /**
   * 删除发票信息
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    PurchaseInvoice invoice = purchaseInvoiceRepositoryImpl.getById(id);
    invoice.destroy();
    purchaseInvoiceRepositoryImpl.remove(invoice);
    this.publicEvent(PurchaseInvoiceEvent.buildDestroy(new Object[]{invoice}));
  }

  /**
   * 签收
   *
   * @param id
   */
  @Override
  public void confirm(Long id) {
    PurchaseInvoice invoice = purchaseInvoiceRepositoryImpl.getById(id);
    invoice.confirm();
    purchaseInvoiceRepositoryImpl.update(invoice);
    this.publicEvent(PurchaseInvoiceEvent.purchaseInvoiceConfirm(new Object[]{invoice}));
  }

  /**
   * 取消签收
   *
   * @param id
   */
  @Override
  public void cancel(Long id) {
    PurchaseInvoice invoice = purchaseInvoiceRepositoryImpl.getById(id);
    invoice.cancel();
    purchaseInvoiceRepositoryImpl.update(invoice);
    this.publicEvent(PurchaseInvoiceEvent.purchaseInvoiceUnconfirm(new Object[]{invoice}));
  }

  /**
   * 解锁
   *
   * @param id
   */
  @Override
  public void unLock(Long id) {
    PurchaseInvoice invoice = purchaseInvoiceRepositoryImpl.getById(id);
    invoice.unLock();
    purchaseInvoiceRepositoryImpl.update(invoice);
  }
}
